package com.letv.core.parser;

import org.json.JSONObject;

import android.text.TextUtils;

import com.letv.core.bean.VideoFileBean.VideoSchedulingAddress;

/**
 * 调度地址解析器
 * 
 * @author zhuqiao
 *
 */
public class VideoSchedulingAddressParser extends LetvMobileParser<VideoSchedulingAddress> {
    private final String MAINURL = "mainUrl";

    private final String BACKURL0 = "backUrl0";

    private final String BACKURL1 = "backUrl1";

    private final String BACKURL2 = "backUrl2";

    private final String FILESIZE = "filesize";

    private final String STOREPATH = "storePath";

    private boolean mNeedPay;

    public VideoSchedulingAddressParser(boolean needPay) {
        mNeedPay = needPay;
    }

    private String replaceUrl(String url) {
        if (url.contains("&format=")) {
            String format = "&format=1";
            for (int i = 0; i < 3; i++) {
                String oldFormat = "&format=" + i;
                if (url.contains(oldFormat)) {
                    url = url.replace(oldFormat, format);
                    break;
                }
            }
        }

        if (url.contains("&expect=")) {
            String expect = "&expect=3";
            for (int i = 0; i < 6; i++) {
                String oldExpect = "&expect=" + i;
                if (url.contains(oldExpect)) {
                    url = url.replace(oldExpect, expect);
                    break;
                }
            }
        }

        if (url.contains("&hwtype=iphone")) {
            url = url.replace("&hwtype=iphone", "&hwtype=un");
        } else {
            url += "&hwtype=un";
        }

        url += "&termid=2&pay=" + (mNeedPay ? 1 : 0) + "&ostype=android";

        return url;
    }

    @Override
    protected VideoSchedulingAddress parse(JSONObject data) throws Exception {
        if (isNull(data)) {
            return null;
        }

        VideoSchedulingAddress address = new VideoSchedulingAddress();

        if (has(data, MAINURL)) {
            String url = getString(data, MAINURL);
            if (!TextUtils.isEmpty(url)) {
                url = replaceUrl(url);
            }

            address.setMainUrl(url);
        }

        if (has(data, BACKURL0)) {
            String url = getString(data, BACKURL0);
            if (!TextUtils.isEmpty(url)) {
                url = replaceUrl(url);
            }

            address.setBackUrl0(url);
        }

        if (has(data, BACKURL1)) {
            String url = getString(data, BACKURL1);
            if (!TextUtils.isEmpty(url)) {
                url = replaceUrl(url);
            }

            address.setBackUrl1(url);
        }

        if (has(data, BACKURL2)) {
            String url = getString(data, BACKURL2);
            if (!TextUtils.isEmpty(url)) {
                url = replaceUrl(url);
            }

            address.setBackUrl2(url);
        }

        address.setFilesize(getLong(data, FILESIZE));
        address.setStorepath(getString(data, STOREPATH));

        return address;
    }

}
